package com.dream.testtubebaby.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.activity.LookupImageActivity;
import com.dream.testtubebaby.data.bbs.BbsReplyNode;
import com.dream.testtubebaby.utils.AppUtil;
import com.dream.testtubebaby.utils.FaceTool;
import com.dream.testtubebaby.utils.ImageLoader;
import com.dream.testtubebaby.utils.TextUtil;

public class BbsReplayToReplayAdapter extends BaseAdapter {
	List<BbsReplyNode> mDatas;
	Context context;
	private TextUtil mTextUtil;

	public BbsReplayToReplayAdapter(Context context, List<BbsReplyNode> items) {
		this.context = context;
		this.mDatas = items;
		mTextUtil = new TextUtil(FaceTool.getInstance(), context);
	}

	public void setDatas(List<BbsReplyNode> mDatas) {
		this.mDatas.clear();
		this.mDatas.addAll(mDatas);
	}

	public void addAll(List<BbsReplyNode> mDatas) {
		this.mDatas.addAll(mDatas);
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		LayoutInflater layoutInflater = LayoutInflater.from(context);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(
					R.layout.bbs_replay_to_replay_listview, null);
			viewHolder.bbs_replay_to_replay_name = (TextView) convertView
					.findViewById(R.id.bbs_replay_to_replay_name);
			viewHolder.bbs_replay_to_replay_time = (TextView) convertView
					.findViewById(R.id.bbs_replay_to_replay_time);
			viewHolder.bbs_replay_to_replay_content = (TextView) convertView
					.findViewById(R.id.bbs_replay_to_replay_content);
			viewHolder.bbs_detail_item_images_layout = (LinearLayout) convertView
					.findViewById(R.id.images_layout);
			viewHolder.bbs_detail_item_reply_image_one = (ImageView) convertView
					.findViewById(R.id.reply_image_one);
			viewHolder.bbs_detail_item_reply_image_two = (ImageView) convertView
					.findViewById(R.id.reply_image_two);
			viewHolder.bbs_detail_item_reply_image_three = (ImageView) convertView
					.findViewById(R.id.reply_image_three);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (mDatas.get(position).getImageUrls().length > 0) {
			viewHolder.bbs_detail_item_images_layout
					.setVisibility(View.VISIBLE);
			int lenth = mDatas.get(position).getImageUrls().length;
			for (int i = 0; i < lenth; i++) {
				if (i == 0) {
					ImageLoader.getInstance().loadNetWorkImage(
							AppUtil.getCompletePhotoUrl(mDatas.get(position).getImageUrls()[i]),
							viewHolder.bbs_detail_item_reply_image_one);
				} else if (i == 1) {
					ImageLoader.getInstance().loadNetWorkImage(
							AppUtil.getCompletePhotoUrl(mDatas.get(position).getImageUrls()[i]),
							viewHolder.bbs_detail_item_reply_image_two);
				} else if (i == 2) {
					ImageLoader.getInstance().loadNetWorkImage(
							AppUtil.getCompletePhotoUrl(mDatas.get(position).getImageUrls()[i]),
							viewHolder.bbs_detail_item_reply_image_three);
				}
			}
		}
		viewHolder.bbs_detail_item_reply_image_one
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startLookUpImage(position, 0);
					}
				});
		viewHolder.bbs_detail_item_reply_image_two
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startLookUpImage(position, 1);
					}
				});
		viewHolder.bbs_detail_item_reply_image_three
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startLookUpImage(position, 2);
					}
				});

		viewHolder.bbs_replay_to_replay_name.setText(mDatas.get(position)
				.getUsername());
		CharSequence time_string = DateUtils.getRelativeTimeSpanString(mDatas
				.get(position).getReply_time(), System.currentTimeMillis(),
				DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);

		viewHolder.bbs_replay_to_replay_time.setText(time_string);
		viewHolder.bbs_replay_to_replay_content.setText(mTextUtil
				.replace(mDatas.get(position).getContent()));
		return convertView;
	}

	private void startLookUpImage(int position, int i) {
		if (i < mDatas.get(position).getImageUrls().length) {
			Intent intent = new Intent("com.intent.action.lookup.image");
			intent.putExtra(LookupImageActivity.LOOKUP_IMAGE_PATH_KEY,
					AppUtil.getCompletePhotoUrl(mDatas.get(position).getImageUrls()[i]));
			intent.putExtra(LookupImageActivity.LOOKUP_IMAGE_TYPE,
					LookupImageActivity.NETWORK_IMAGE);
			context.startActivity(intent);
		}
	}

	class ViewHolder {
		TextView bbs_replay_to_replay_name;
		TextView bbs_replay_to_replay_time;
		TextView bbs_replay_to_replay_content;
		LinearLayout bbs_detail_item_images_layout;
		ImageView bbs_detail_item_reply_image_one;
		ImageView bbs_detail_item_reply_image_two;
		ImageView bbs_detail_item_reply_image_three;
	}
}
